package com.controller;


import io.swagger.annotations.Api;
import lombok.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;


@Api(tags = "邮件发送通用接口")
@RestController
public class EmailController {
    @Autowired
    private JavaMailSender jms;

    @org.springframework.beans.factory.annotation.Value("${Spring.mail.username}")
    private String username;

    @RequestMapping(value = "/SendEmail",method = RequestMethod.POST)
        public String SendEmail(){
            System.out.println("进入 发送普通邮件的方法");
            String str=null;
            try {
                //1.声明邮箱对象
                SimpleMailMessage message = new SimpleMailMessage();
                //2.通过邮箱对象设置下相关参数
                message.setFrom(username);
                message.setTo("2944451296@qq.com");
                message.setSubject("发送一封简单的邮件");
                message.setText("欢迎来到方向2 全军出击");
                //3.发送邮件
                jms.send(message);
                str = "成功发送邮件！";
            }catch (Exception e){
                System.out.println("简单邮件发送失败！原因为："+e.getMessage());
                str="发送失败";
            }
            return str;
    }

    @RequestMapping(value = "/SendHtmlEmail",method = RequestMethod.POST)
    public String SendHtmlEmail(){
        System.out.println("进入 发送html内容的邮件方法");
        String str="";
        try {
            //1.声明邮箱对象
            MimeMessage message =null;
            message=jms.createMimeMessage();

            MimeMessageHelper html=new MimeMessageHelper(message,true);
            html.setFrom(username);
            html.setTo("2944451296@qq.com");
            html.setSubject("Html的一封邮件");
            //声明带有html命令格式的内容
            StringBuffer sb=new StringBuffer("<p style='color:#42b98e'>使用大数据分析内容，"+"通过html格式将内容发送到网页上。</p>");
            html.setText(sb.toString(),true);

            jms.send(message);

            str="成功发送html邮件！";
        }catch (Exception e){
            System.out.println("html邮件发送失败！原因为："+e.getMessage());
            str="html邮件发送失败";
        }
        return str;
    }

    @RequestMapping(value = "/SendAttachment",method = RequestMethod.POST)
    public String SendAttachment(){
        System.out.println("进入 发送附件的邮件方法");
        String str="";
        try {
            //1.声明邮箱对象
            MimeMessage message =null;
            message=jms.createMimeMessage();

            MimeMessageHelper html=new MimeMessageHelper(message,true,"UTF-8");

            html.setFrom(username);
            html.setTo("2944451296@qq.com");
            html.setSubject("发送带附件的邮件");
            html.setText("详情请下载附件：");
            FileSystemResource file=new FileSystemResource("src\\main\\resources\\trial\\1.docx");
            html.addAttachment("1.docx",file);
            jms.send(message);

            str="成功发送html邮件！";
        }catch (Exception e){
            System.out.println("附件邮件发送失败！原因为："+e.getMessage());
            str="附件邮件发送失败";
        }
        return str;
    }

    @RequestMapping(value = "/SendImgEmail",method = RequestMethod.POST)
    public String SendImgEmail(){
        System.out.println("进入 发送带图片的的网页方法");
        String str="";
        try {
            //1.声明邮箱对象
            MimeMessage message =null;
            message=jms.createMimeMessage();

            MimeMessageHelper html=new MimeMessageHelper(message,true,"UTF-8");

            html.setFrom(username);
            html.setTo("2944451296@qq.com");
            html.setSubject("发送带图片的网页");

            String txt = "<html><body>I'm coming!<img src='cid:img'/></body></html>";
            html.setText(txt);
            FileSystemResource fileImg=new FileSystemResource("src\\main\\resources\\img\\cat.jpg");
            html.addAttachment("cat.jpg",fileImg);

            jms.send(message);

            str="成功发送带图片的网页邮件！";
        }catch (Exception e){
            System.out.println("附件邮件发送失败！原因为："+e.getMessage());
            str="发送带有图片的网页失败";
        }
        return str;
    }

}
